local skel = fk.CreateSkill {
  name = "emo__wenxin",
  tags = {Skill.Compulsory},
}

Fk:loadTranslationTable{
  ["emo__wenxin"] = "问心",
  [":emo__wenxin"] = "锁定技，游戏开始时，你将牌堆中随机九张牌标记为“问心”（“问心”牌离开牌堆时移除标记）。每当你进入濒死状态时，若牌堆有“问心”牌，你摸一张“问心”牌并将体力值回复至1点。",

  ["@[private]$emo__wenxin"] = "问心",
}

local U = require "packages.utility.utility"

---@param player ServerPlayer
local updataWenxin = function (player)
  local room = player.room
  local mark = U.getPrivateMark(player, "$emo__wenxin")
  local ids = table.filter(mark, function (id)
    return room:getCardArea(id) == Card.DrawPile
  end)
  if #ids == 0 then
    room:setPlayerMark(player, "@[private]$emo__wenxin", 0)
  else
    U.setPrivateMark(player, "$emo__wenxin", ids)
  end
end

skel:addEffect(fk.GameStart, {
  anim_type = "drawcard",
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(skel.name)
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local ids = table.random(room.draw_pile, 9)
    if #ids == 0 then return end
    U.setPrivateMark(player, "$emo__wenxin", ids)
    updataWenxin(player)
  end,
})

skel:addEffect(fk.EnterDying, {
  anim_type = "defensive",
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(skel.name) and target == player and #U.getPrivateMark(player, "$emo__wenxin") > 0
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local mark = U.getPrivateMark(player, "$emo__wenxin")
    room:obtainCard(player, table.random(mark), false, fk.ReasonPrey, player, skel.name)
    updataWenxin(player)
    if not player.dead then
      room:recover { num = 1 - player.hp, skillName = skel.name, who = player , recoverBy = player}
    end
  end,
})

-- 问心牌离开牌堆时，清除记录
skel:addEffect(fk.AfterCardsMove, {
  can_refresh = function(self, event, target, player, data)
    if #U.getPrivateMark(player, "$emo__wenxin") == 0 then return false end
    for _, move in ipairs(data) do
      for _, info in ipairs(move.moveInfo) do
        if info.fromArea == Card.DrawPile then
          return true
        end
      end
    end
  end,
  on_refresh = function(self, event, target, player, data)
    updataWenxin(player)
  end,
})

skel:addEffect(fk.AfterDrawPileShuffle, {
  can_refresh = function (self, event, target, player, data)
    return #U.getPrivateMark(player, "$emo__wenxin") > 0
  end,
  on_refresh = function (self, event, target, player, data)
    updataWenxin(player)
  end,
})

skel:addLoseEffect(function (self, player, is_death)
  player.room:setPlayerMark(player, "@[private]$emo__wenxin", 0)
end)


return skel
